import {
  getApplySwiper,
  smsSend,
  applySubmit
} from '../../../utils/networkRequest.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swipers: [], //轮播图
    checkChopInfo: null,
    downtime: 60,
    inputPhoneValue: "", //手机号 
    verCode: "", //验证码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let self = this;
    getApplySwiper((r) => {
      if (r.code == 0) {
        self.setData({
          swipers: r.data.lunbo
        })
      } else {
        wx.showToast({
          icon: 'none',
          title: r.message
        })
      }
    })
  },
  //跳转至选择门店
  goCheckShop: function() {
    wx.navigateTo({
      url: '../checkShop/checkShop',
    })
  },
  //获取手机号
  getPhone: function(e) {
    this.setData({
      inputPhoneValue: e.detail.value
    })
  },
  //获取验证码
  getVcode: function(e) {
    this.setData({
      verCode: e.detail.value
    })
  },
  //发送验证码
  onSendVcode: function() {
    let self = this;
    let {
      inputPhoneValue
    } = this.data
    if (inputPhoneValue == "") {
      wx.showToast({
        icon: "none",
        title: '手机号不能为空',
      })
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(inputPhoneValue)) {
      wx.showToast({
        icon: "none",
        title: '手机号格式不正确',
      })
    } else {
      smsSend(inputPhoneValue, (r) => {
        if (r.code == 0) {
          this.setData({
            downtime: --self.data.downtime
          })
          let siv = setInterval(() => {
            if (self.data.downtime > 1) {
              self.setData({
                downtime: --self.data.downtime
              })
            } else {
              clearInterval(siv);
              self.setData({
                downtime: 60
              })
            }

          }, 1000)
        } else {
          wx.showToast({
            icon: 'none',
            title: r.message
          })
        }
      })
    }

  },
  //立即领取
  onSubmit: function() {
    let {
      checkChopInfo,
      inputPhoneValue,
      verCode
    } = this.data;
    debugger;
    if (!checkChopInfo || !checkChopInfo.id) {
      wx.showToast({
        icon: "none",
        title: '请先选择领取门店',
      })
    } else if (inputPhoneValue == "") {
      wx.showToast({
        icon: "none",
        title: '手机号不能为空',
      })
    } else if (!/^1([358][0-9]|4[579]|66|7[0135678]|9[89])[0-9]{8}$/.test(inputPhoneValue)) {
      wx.showToast({
        icon: "none",
        title: '手机号格式不正确',
      })
    } else if (verCode == "") {
      wx.showToast({
        icon: "none",
        title: '请输入验证码',
      })
    } else {
      applySubmit(inputPhoneValue, verCode, checkChopInfo.id, (r) => {
        if (r.code == 0) {
          debugger;
          wx.navigateTo({
            url: '../applysuccess/applysuccess?shopInfo=' + JSON.stringify(checkChopInfo) + "&word=" + r.data.adword.content,
          })
        } else {
          wx.showToast({
            icon: "none",
            title: r.message,
          })
        }

      })
    }

  }
})